package cal;
import java.io.*;
import java.util.Date;
import javax.xml.parsers.SAXParserFactory;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class IndexHTML {
	IndexReader reader;		  // existing index
	IndexWriter writer;		  // new index being built
	TermEnum uidIter;		  // document id iterator
	Translate trans;
	static  IndexHTML instance=null;
	public boolean flag_for_index =  false;
	int numIndexed = 0;
	public boolean flag = true;//true: sougou; false: threeMonth
	String indexAdd = "similar_index";
	
	String addName = "F:\\j2me\\PicSearch\\file\\mydic.dic";
	public static IndexHTML get_instance()
	{
		if(instance==null)
		{
			instance=new IndexHTML();
			return instance;
		}
		else
			return  instance;
	}
	public IndexHTML() {}
	 // open an index and start file directory traversal
    public  void index(String sogou,String three,String indexPath) {
        try {
        
        IKAnalyzer analyzer = new IKAnalyzer();
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_31,analyzer);
        File indexDir = new File(indexPath);
        Directory fsDir = FSDirectory.open(indexDir);
        
        writer = new IndexWriter(fsDir,conf); 
        writer.deleteAll();
        long start = new Date().getTime();
       // writer.setUseCompoundFile(false);
        
      flag = true;
         File dataDir = new File(sogou);
       if (!dataDir.exists()) {
           throw new IOException(dataDir
               + " does not exist.");
       }
       indexFile(writer, dataDir,flag);
       
        flag = false;
        dataDir = new File(three);
       if (!dataDir.exists()) {
            throw new IOException(dataDir
                + " does not exist.");
       }
       indexFile(writer,dataDir,flag);
        
        numIndexed = writer.numDocs();
        
        writer.optimize();
        
        writer.close();
        fsDir.close();
        setAdd();
        long end = new Date().getTime();
        System.out.println("Indexing " + numIndexed + " files took "
                + (end - start) + " milliseconds");
        
        }catch (IOException e){
        	System.out.println(e.getMessage());
        }
        return;
    }
    
    // method to actually index file using Lucene
    private void indexFile(IndexWriter writer, File f,boolean flag)
        throws IOException {
        if (f.isHidden() || !f.exists() || !f.canRead()) {   return;   }
        System.out.println(f.getPath());
        trans = new Translate();
        trans.init(this,f,flag);
        SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			javax.xml.parsers.SAXParser saxParser = factory.newSAXParser();
			saxParser.parse(f,trans);
		}catch(Throwable t) {
			t.printStackTrace();
		}
    }
    
    public void setAdd() {//add similar word
    	
    	try {
    	File file = new File(addName);
    		
    	FileReader ir = new FileReader(addName);
    	
    	BufferedReader irr = new BufferedReader(ir);
        File indexDir = new File(indexAdd);
        Directory fsDir = FSDirectory.open(indexDir);
      
        //Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
        Analyzer analyzer = new IKAnalyzer();
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_31,analyzer);
        IndexWriter indexWriter = new IndexWriter(fsDir, conf);    
 
        indexWriter.deleteAll();
        
        String line = new String();               
        int count = 0;
      
        while ((line = irr.readLine()) != null) {   
        	String[] word= line.split(" ");
            
          //  System.out.println(line+"\t"+word.length);
            
            for (int i = 1; i<word.length; i++) {
            	Document document = new Document();
            	Field FieldName = new Field("tag",word[i].trim(),Field.Store.YES, Field.Index.ANALYZED);
            	document.add(FieldName);
            	FieldName = new Field("name",word[0].trim(),Field.Store.YES, Field.Index.ANALYZED);
                document.add(FieldName);
                String simi = word[0].trim()+" ";
                for (int j = 1; j<word.length; j++)  {
                	if (j == i) continue;
                	simi += word[j].trim()+" ";
                }
                FieldName = new Field("simi",simi,Field.Store.YES, Field.Index.NO);
                document.add(FieldName);
                indexWriter.addDocument(document);
                count ++;
                System.out.println(word[0]+" "+word[i]);
            }
            
            
            //System.out.println(word[0]);
          
           // System.out.println("count");
	    }   
	
	    indexWriter.optimize();
	
	     indexWriter.close();
	
	    irr.close();  
	    ir.close();
	    fsDir.close();
	    //System.out.println("the similarity count:"+count);
	    }catch(Exception e) {
	    	e.printStackTrace();
	    }
	
	    }
    
}